/**
 *
 * by littlefean
 */


window.onload = function () {
    let outEle = document.querySelector(`.output`);
    let aEle = document.querySelector(`.a`);
    let bEle = document.querySelector(`.b`);
    let cEle = document.querySelector(`.c`);

    function test() {
        let a = +aEle.value;
        let b = +bEle.value;
        let c = +cEle.value;
        // alert(`${a}, ${b}, ${c}`);
        let delta = b ** 2 - 4 * a * c;
        if (isNaN(delta)) {
            outEle.innerHTML = "输入有错误";
        } else if (a === 0) {
            outEle.innerHTML = "当前是一个一次方程";
            if (b === 0) {
                outEle.innerHTML += "<br>没有解";
            } else {
                outEle.innerHTML += "<br>解是" + (-c / b).toString();
            }
        } else {
            // 开始进行判别式
            if (delta > 0) {
                outEle.innerHTML = "有两个实根";
                let res1 = -b + (delta) ** 0.5 / (4 * a);
                let res2 = -b - (delta) ** 0.5 / (4 * a);
                outEle.innerHTML += "<br>解是" + res1.toString() + ", " + res2.toString();
            } else if (delta === 0) {
                outEle.innerHTML = "有一个实根";
                let res = -b / (4 * a);
                outEle.innerHTML += "<br>解是" + res.toString();
            } else {
                outEle.innerHTML = "没有实根";
            }
        }

    }

    document.querySelector(`.start`).addEventListener("click", () => {
        test();
    });
}
